package edu.paper.job.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.paper.job.domain.AjaxResult;
import edu.paper.job.domain.Applicant;
import edu.paper.job.domain.Employee;
import edu.paper.job.domain.Job;
import edu.paper.job.domain.Resume;
import edu.paper.job.service.ApplicantService;
import edu.paper.job.service.EmployeeService;
import edu.paper.job.service.JobService;
import edu.paper.job.service.ResumeService;

/**
 * applicant控制器
 * @author bowen
 *
 */
@Controller
public class ApplicantController {
	
	@Autowired
	private ApplicationContext applicationContext;
	
	//applicant业务逻辑
	@Autowired
	private ApplicantService applicantService;
	
	//简历业务逻辑
	@Autowired
	private ResumeService resumeService;
	
	@Autowired
	private EmployeeService employeeService;
	
	/**
	 * 登录，判断用户名，密码是否错误
	 */
	@RequestMapping("/applicant/login.action")
	public @ResponseBody AjaxResult login(Applicant applicant, Model model) {
		if(applicant.getUsername() == null) {
			applicant.setUsername("");
		}
		if(applicant.getPassword() == null) {
			applicant.setPassword("");
		}
		AjaxResult result = new AjaxResult();		//ajax请求结果
		Applicant loginApplicant = applicantService.login(applicant);	//登陆查询是否有该用户
		if(loginApplicant != null) {
			//如果存在，就告诉他有该用户
			result.setResult(AjaxResult.SUCCESS);
		}else {
			//如果不存在，就告诉他账号密码错误
			result.setResult(AjaxResult.FAIL);
		}
		return result;
	}
	
	/**
	 * 去求职者主页面
	 * @param applicant
	 * @return
	 */
	@RequestMapping("/applicant/home.action") 
	public String home(Applicant applicant, Boolean rem, Model model, HttpServletResponse response) {
		if(rem != null && rem) {//判断是否要记住求职者
			Cookie usernameCookie = new Cookie("username", applicant.getUsername());
			Cookie passwordCookie = new Cookie("password", applicant.getPassword());
			response.addCookie(usernameCookie);
			response.addCookie(passwordCookie);
		}
		//找到数据库中的求职者，主要填充applicantId属性，因为传的没这个属性
		if(applicantService.find(applicant).size() > 0) {
			applicant = applicantService.find(applicant).get(0);
		}
		model.addAttribute("applicant", applicant);
		return "applicant/home.jsp";
	}
	
	
	
	/**
	 * 跳转到注册页面
	 * @return
	 */
	@RequestMapping("/applicant/toEnroll.action")
	public String toEnroll() {
		return "applicant/jApplicantRegister.jsp";
	}
	
	/**
	 * 注册求职者，先查询用户名是否重复，若重复就提醒他，若不重复就注册
	 * @param applicant
	 * @param model
	 */
	@RequestMapping("/applicant/enroll.action")
	public @ResponseBody AjaxResult enroll(Applicant applicant, Model model) {
		AjaxResult result = new AjaxResult();		//ajax结果
		
		if(applicantService.register(applicant)) {
			//若用户名不重复，就注册,并告诉他注册成功
			result.setResult(AjaxResult.SUCCESS);
			
		}else {
			//若用户名重复，就告诉他用户名重复
			result.setResult(AjaxResult.FAIL);
		}
		return result;
		
	}
	/**
	 * 列出求职者没被删除的简历
	 */
	@RequestMapping("/applicant/list/resume/{applicantId}.action")
	public String list(@PathVariable Long applicantId, Model model) {
		//得到没被删除的简历
		Resume params = new Resume();
		params.setApplicantId(applicantId);
		params.setIsDeleted(false);
		List<Resume> resumes = resumeService.find(params);
		//传递给页面
		model.addAttribute("dataList", resumes);
		model.addAttribute("applicantId", applicantId);
		return "applicant/jResumeList.jsp";
	}
	
	/**
	 * 浏览求职者简历的信息
	 * @return String
	 */
	@RequestMapping("/applicant/view/resume/{resumeId}.action")
	public String view(@PathVariable Long resumeId, Model model) {
		Resume resume = resumeService.get(resumeId);		//获得要浏览的简历
		model.addAttribute("resume", resume);		//传递到界面上
		return "applicant/jResumeView.jsp";			//返回视图
	}
	
	/**
	 * 去写简历的页面
	 * @return String
	 */
	@RequestMapping("/applicant/toAdd/resume/{applicantId}.action")
	public String toAddResume(@PathVariable Long applicantId, Model model) {
		//得到写简历的求职者基本信息
		Applicant applicant = applicantService.get(applicantId);		
		//传递给添加简历的页面
		model.addAttribute("applicant", applicant);
		//返回视图
		return "applicant/jResumeAdd.jsp";
	}
	
	/**
	 * 保存简历
	 * @return String
	 */
	@RequestMapping("/applicant/save/resume/{applicantId}.action")
	public String saveResume(Resume resume) {
		if(resume.getIsPublic() == null) {
			resume.setIsPublic(false);
		}
		if(resume.getDescription() == null) {
			resume.setDescription("");
		}
		//保存
		resumeService.insertResume(resume);
		//回到列出简历的页面
		return "redirect:/applicant/list/resume/"+ resume.getApplicantId() +".action";
	}
	
	/**
	 * 去更新简历的界面
	 * @param id
	 * @param model
	 * @return String
	 */
	@RequestMapping("/applicant/toUpdate/resume/{resumeId}.action")
	public String toUpdate(@PathVariable Long resumeId, Model model) {
		//得到要更新的简历
		Resume resume = resumeService.get(resumeId);
		//把数据给页面
		model.addAttribute("resume", resume);
		//返回视图
		return "applicant/jResumeUpdate.jsp";
	}
	
	/**
	 * 更新简历
	 * @param resume
	 * @return String
	 */
	@RequestMapping("/applicant/update/resume/{resumeId}.action")
	public String update(Resume resume) {
		if(resume.getIsPublic() == null) {
			resume.setIsPublic(false);
		}
		if(resume.getDescription() == null) {
			resume.setDescription("");
		}
		resumeService.updateResume(resume);
		//返回列表视图
		return "redirect:/applicant/list/resume/"+ resume.getApplicantId() +".action";
	}
	
	/**
	 * 删除简历
	 * @param resumeId
	 * @return
	 */
	@RequestMapping("/applicant/delete/resume/{resumeId}.action")
	public String delete(@PathVariable Long resumeId) {
		//假删除简历
		Resume resume = resumeService.get(resumeId);
		resumeService.deleteResume(resume);
		return "redirect:/applicant/list/resume/"+ resume.getApplicantId() +".action";
		
	}
	
	/**
	 * 去模糊查询发布的简历
	 * @param model
	 * @return 模糊查询界面的路径
	 */
	@RequestMapping("/applicant/toQuery/resume.action")
	public String toQuery(Model model) {
		//找到数据
		Resume resume = applicationContext.getBean(Resume.class);
		List<Resume> dataList = resumeService.queryResume(resume);
		model.addAttribute("dataList", dataList);
		return "applicant/jResumeQuery.jsp";
	}
	
	/**
	 * 根据条件模糊查询发布的简历
	 * @param resume
	 * @param model
	 * @return	查询的结果视图
	 */
	@RequestMapping("/applicant/query/resume.action")
	public String query(Resume resume, Model model) {
		//查询
		List<Resume> dataList = resumeService.queryResume(resume);
		model.addAttribute("dataList", dataList);
		return "applicant/jResumeQueryTable.jsp";
	}
	
	/**
	 * 去选择简历申请工作
	 * @param applicantId
	 * @param jobId
	 * @return 返回选择简历的视图的路径
	 */
	@RequestMapping("/applicant/toApply/job/{applicantId}/{jobId}.action")
	public String toApply(@PathVariable Long applicantId,@PathVariable Long jobId, Model model) {
		//找到求职者的简历
		Resume resumeParams = applicationContext.getBean(Resume.class);
		resumeParams.setApplicantId(applicantId);
		List<Resume> dataList = resumeService.listApplicantResumes(resumeParams);
		//将数据传给选择简历页面
		model.addAttribute("dataList", dataList);
		model.addAttribute("jobId", jobId);
//		model.addAttribute("applicantId", applicantId);
		return "applicant/jResumeSelect.jsp";
	}
	
	/**
	 * 申请工作，一个工作只能被同一个求职者申请一次
	 * @param resumeId
	 * @param jobId
	 * @return
	 */
	@RequestMapping("/applicant/apply/job.action")
	public @ResponseBody AjaxResult apply(Long applicantId, Long resumeId, Long jobId) {
		//先查询该职业是否被该求职者申请过
		Resume resume = applicationContext.getBean(Resume.class);
		resume.setId(resumeId);
		resume.setApplicantId(applicantId);
		Job job = applicationContext.getBean(Job.class);
		job.setId(jobId);
		Employee params = applicationContext.getBean(Employee.class);
		params.setResume(resume);
		params.setJob(job);
		params.setIsApplicantDeleted(false);
		List<Employee> employees = employeeService.find(params);
		AjaxResult result = applicationContext.getBean(AjaxResult.class);
		if(employees.size() == 0) {
			//如果没申请过
			Employee employee = params;
			employeeService.insertEmployee(employee);
			result.setResult("申请成功");
			return result;
		}
		result.setResult("您已申请过该职业");
		return result;
	}
	
	/**
	 * 浏览求职者的求职进展
	 * @param applicantId
	 * @param model
	 * @return
	 */
	@RequestMapping("/applicant/list/employee/{applicantId}.action")
	public String listEmployees(@PathVariable Long applicantId, Model model) {
		/*
		 * 获取求职者的求职进展
		 */
		//准备搜索条件
		Resume resume = applicationContext.getBean(Resume.class);
		resume.setApplicantId(applicantId);
		Employee params = applicationContext.getBean(Employee.class);
		params.setResume(resume);
		List<Employee> dataList = employeeService.listApplicantEmployee(params);
		model.addAttribute("dataList", dataList);
		return "applicant/jEmployeeList.jsp";
	}
	
	/**
	 * 删除求职进展
	 * @param applicantId
	 * @param employeeId
	 * @return 返回求职进展列表界面
	 */
	@RequestMapping("/applicant/delete/employee/{applicantId}/{employeeId}.action")
	public String deleteEmployee(@PathVariable Long applicantId, @PathVariable Long employeeId) {
		Employee employee = applicationContext.getBean(Employee.class);
		employee.setId(employeeId);
		employeeService.deleteEmployeeByApplicant(employee);
		return "redirect:/applicant/list/employee/" + applicantId +".action";
		
	}
	
}
